﻿using System.Collections.Generic;
using IceCS.GoodsSale.DAL;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.BLL
{
    public class UserManager
    {
        //定义用户登录后，用于存放当前用户信息的静态字符串数组
        public static string[] userInfo = new string[3];
        //定于用于判断是登录商品销售前台还是后台管理的字符串变量
        public static string loginType = "";
        PublicMethodsAccesser publicMethodsDAL = new PublicMethodsAccesser();
        UserAccesser userDAL = new UserAccesser();

        //调用DAL层的SelectUser()方法，判断用户登录时属于哪种情况
        public string UserLogin(string userName, string userPassword)
        {
            string md5UserPassword = publicMethodsDAL.GetMd5Str(userPassword);
            User user = userDAL.SelectUser(userName);
            if (user == null)
            {
                return "UserName Error";
            }
            else if (user.UserPassword == md5UserPassword)
            {
                userInfo[0] = user.UserName;
                userInfo[1] = user.UserPassword;
                userInfo[2] = user.UserType;
                return "OK";
            }
            else
            {
                return "UserPassword Error";
            }
        }

        //判断用户是否已存在于数据库的方法
        public bool UserIsExist(string userName)
        {
            User user = userDAL.SelectUser(userName);
            if (user != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //修改用户密码的方法
        public bool ChangeUserPassword(string userName, string userPassword)
        {
            if (userDAL.UpdateUserPassword(userName, userPassword))
            {
                userInfo[1] = publicMethodsDAL.GetMd5Str(userPassword);
                return true;
            }
            return false;
        }

        //返回用户名列表的方法
        public List<User> GetUserNames()
        {
            return userDAL.SelectUserNames();
        }
    }
}
